import threading


class ThreadPool:
    __thread_size = 0
    __item_pool = []
    __thread_pool = []
    __stop = False
    __stop_immediately = False

    def __init__(self, thread_size):
        self.__thread_size = thread_size

    def start(self):
        for i in range(0, self.__thread_size):
            t = threading.Thread(target=self.__looping_run)
            t.start()
            self.__thread_pool.append(t)

    def wait_all_complete(self):
        self.__stop = True
        for t in self.__thread_pool:
            t.join()

    def start_wait_all_complete(self):
        self.start()
        self.wait_all_complete()

    def stop_immediately(self):
        self.__stop_immediately = True

    def __looping_run(self):
        while True:
            if self.__stop_immediately:
                break
            if self.__item_pool.__len__() > 0:
                try:
                    item = self.__item_pool.pop()
                    self._run(item)
                except IndexError:
                    pass
            else:
                if self.__stop:
                    break

    def _run(self, item):
        pass

    def push_task_item(self, item):
        self.__item_pool.append(item)

    def push_task_items(self, items):
        self.__item_pool.extend(items)
